//
//  GMTorrentViewController.m
//  gentleman
//
//  Created by 范月成 on 2017/12/14.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMTorrentViewController.h"
#import "GMPlaceHolderView.h"
#import "GMTorrentListModel.h"

@interface GMTorrentViewController ()

@property (nonatomic, strong, readwrite) GMArtwork *artwork;

@property (nonatomic, strong) GMTorrentListModel *listModel;
@property (nonatomic, strong) UITableView *torrentList;
@property (nonatomic, strong) GMPlaceHolderView *torrentEmpty;

@end

@implementation GMTorrentViewController

+ (void)load {
    [MGJRouter registerURLPattern:kRouterTorrentList
                        toHandler:^(NSDictionary *routerParameters) {
                            GMArtwork *artwork = routerParameters[MGJRouterParameterUserInfo][@"artwork"];
                            GMTorrentViewController *torrent = [[GMTorrentViewController alloc] initWithArtwork:artwork];
                
                            [[UIViewController currentNavigationViewController] pushViewController:torrent animated:YES];
                        }];
}

- (instancetype)initWithArtwork:(GMArtwork *)artwork {
    if (self = [super init]) {
        self.artwork = artwork;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"种子列表";
    
    [self requestDatas];
}

- (void)initSubviews {
    [super initSubviews];
    [self.view addSubview:self.torrentList];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.torrentList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(self.safeAreaTop);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(kScreenHeight-self.safeAreaTop-self.safeAreaBottom);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

- (void)requestDatas {
    [self.listModel getTorrentListWithGid:self.artwork.gid token:self.artwork.token completed:^(NSError *error, NSMutableArray *artworks) {
        if (error) {
            self.torrentEmpty.title = error.domain;
            [GMUIHelper alertWithTitle:error.domain message:nil];
        }
        [self.torrentList.mj_header endRefreshing];
        [self.torrentList reloadData];
    }];
}

#pragma mark - getter
- (GMTorrentListModel *)listModel {
    if (!_listModel) {
        _listModel = [[GMTorrentListModel alloc] init];
    }
    return _listModel;
}

- (UITableView *)torrentList {
    if (!_torrentList) {
        _torrentList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _torrentList.delegate = self.listModel;
        _torrentList.dataSource = self.listModel;
        _torrentList.tableFooterView = [UIView new];
        _torrentList.rowHeight = 44.f;
        [_torrentList disabledEstimatedHeight];
        
        @weakify(self)
        _torrentList.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            [self requestDatas];
        }];
        
        [_torrentList gm_configureWithPlaceHolderBlock:^UIView * _Nonnull(UITableView * _Nonnull sender) {
            @strongify(self)
            return self.torrentEmpty;
        } normalBlock:^(UITableView * _Nonnull sender) {
            
        }];
    }
    return _torrentList;
}

- (GMPlaceHolderView *)torrentEmpty {
    if (!_torrentEmpty) {
        _torrentEmpty = [[GMPlaceHolderView alloc] initWithFrame:self.torrentList.bounds];
        _torrentEmpty.title = @"木有数据";
        @weakify(self)
        _torrentEmpty.refreshBlock = ^{
            @strongify(self)
            [self requestDatas];
        };
    }
    return _torrentEmpty;
}


@end
